<?php
namespace App\Library\Dysmsapi;
/**
 * Created by PhpStorm.
 * User: xinghuo
 * Date: 2017/6/24
 * Time: 上午11:46
 */
use Dysmsapi\Request\V20170525\SendSmsRequest;
use Flc\Alidayu\Client;
use Flc\Alidayu\App;
use Flc\Alidayu\Requests\AlibabaAliqinFcSmsNumSend;

class Sms{
    protected $config = [];
    protected $sendResponse = [];

    /**
     * @return array
     */
    public function getSendResponse()
    {
        return $this->sendResponse;
    }

    /**
     * @param array $sendResponse
     */
    public function setSendResponse($sendResponse)
    {
        $this->sendResponse = $sendResponse;
    }

    public function __construct($config)
    {
        $this->config = $config;
    }

    public function send($mobile, $templateId, $data){

        //此处需要替换成自己的AK信息
        $accessKeyId = $this->config['app_key'];
        $accessKeySecret = $this->config['app_secret'];
        //短信API产品名
        $product = "Dysmsapi";
        //短信API产品域名
        $domain = "dysmsapi.aliyuncs.com";
        //暂时不支持多Region
        $region = "cn-hangzhou";
        define('ENABLE_HTTP_PROXY',0);
        //初始化访问的acsCleint
        \DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
        $profile = \DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);

        \EndpointProvider::setEndpoints(\DefaultProfile::getEndpoints());
        $acsClient= new \DefaultAcsClient($profile);

        $request = new SendSmsRequest();
        //必填-短信接收号码
        $request->setPhoneNumbers($mobile);
        //必填-短信签名
        $request->setSignName($this->config['signName']);
        //必填-短信模板Code
        $request->setTemplateCode($templateId);
        //选填-假如模板中存在变量需要替换则为必填(JSON格式)
        $request->setTemplateParam(json_encode($data));
        //选填-发送短信流水号
//        $request->setOutId("1234");

        //发起访问请求
        $acsResponse = $acsClient->getAcsResponse($request);

        $this->setSendResponse($acsResponse);
        return $acsResponse;
    }

}
